home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tk8.0 / generic / tkEvent.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-08-15  |  29.0 KB  |  1,039 lines  |  [TEXT/CWIE]

  1. /* 
  2.  * tkEvent.c --
  3.  *
  4.  *    This file provides basic low-level facilities for managing
  5.  *    X events in Tk.
  6.  *
  7.  * Copyright (c) 1990-1994 The Regents of the University of California.
  8.  * Copyright (c) 1994-1995 Sun Microsystems, Inc.
  9.  *
  10.  * See the file "license.terms" for information on usage and redistribution
  11.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  12.  *
  13.  * SCCS: @(#) tkEvent.c 1.20 96/09/20 09:33:38
  14.  */
  15.  
  16. #include "tkPort.h"
  17. #include "tkInt.h"
  18. #include <signal.h>
  19.  
  20. /*
  21.  * There's a potential problem if a handler is deleted while it's
  22.  * current (i.e. its procedure is executing), since Tk_HandleEvent
  23.  * will need to read the handler's "nextPtr" field when the procedure
  24.  * returns.  To handle this problem, structures of the type below
  25.  * indicate the next handler to be processed for any (recursively
  26.  * nested) dispatches in progress.  The nextHandler fields get
  27.  * updated if the handlers pointed to are deleted.  Tk_HandleEvent
  28.  * also needs to know if the entire window gets deleted;  the winPtr
  29.  * field is set to zero if that particular window gets deleted.
  30.  */
  31.  
  32. typedef struct InProgress {
  33.     XEvent *eventPtr;         /* Event currently being handled. */
  34.     TkWindow *winPtr;         /* Window for event.  Gets set to None if
  35.                   * window is deleted while event is being
  36.                   * handled. */
  37.     TkEventHandler *nextHandler; /* Next handler in search. */
  38.     struct InProgress *nextPtr;     /* Next higher nested search. */
  39. } InProgress;
  40.  
  41. static InProgress *pendingPtr = NULL;
  42.                 /* Topmost search in progress, or
  43.                  * NULL if none. */
  44.  
  45. /*
  46.  * For each call to Tk_CreateGenericHandler, an instance of the following
  47.  * structure will be created.  All of the active handlers are linked into a
  48.  * list.
  49.  */
  50.  
  51. typedef struct GenericHandler {
  52.     Tk_GenericProc *proc;    /* Procedure to dispatch on all X events. */
  53.     ClientData clientData;    /* Client data to pass to procedure. */
  54.     int deleteFlag;        /* Flag to set when this handler is deleted. */
  55.     struct GenericHandler *nextPtr;
  56.                 /* Next handler in list of all generic
  57.                  * handlers, or NULL for end of list. */
  58. } GenericHandler;
  59.  
  60. static GenericHandler *genericList = NULL;
  61.                 /* First handler in the list, or NULL. */
  62. static GenericHandler *lastGenericPtr = NULL;
  63.                 /* Last handler in list. */
  64.  
  65. /*
  66.  * There's a potential problem if Tk_HandleEvent is entered recursively.
  67.  * A handler cannot be deleted physically until we have returned from
  68.  * calling it.  Otherwise, we're looking at unallocated memory in advancing to
  69.  * its `next' entry.  We deal with the problem by using the `delete flag' and
  70.  * deleting handlers only when it's known that there's no handler active.
  71.  *
  72.  * The following variable has a non-zero value when a handler is active.
  73.  */
  74.  
  75. static int genericHandlersActive = 0;
  76.  
  77. /*
  78.  * The following structure is used for queueing X-style events on the
  79.  * Tcl event queue.
  80.  */
  81.  
  82. typedef struct TkWindowEvent {
  83.     Tcl_Event header;        /* Standard information for all events. */
  84.     XEvent event;        /* The X event. */
  85. } TkWindowEvent;
  86.  
  87. /*
  88.  * Array of event masks corresponding to each X event:
  89.  */
  90.  
  91. static unsigned long eventMasks[TK_LASTEVENT] = {
  92.     0,
  93.     0,
  94.     KeyPressMask,            /* KeyPress */
  95.     KeyReleaseMask,            /* KeyRelease */
  96.     ButtonPressMask,            /* ButtonPress */
  97.     ButtonReleaseMask,            /* ButtonRelease */
  98.     PointerMotionMask|PointerMotionHintMask|ButtonMotionMask
  99.         |Button1MotionMask|Button2MotionMask|Button3MotionMask
  100.         |Button4MotionMask|Button5MotionMask,
  101.                     /* MotionNotify */
  102.     EnterWindowMask,            /* EnterNotify */
  103.     LeaveWindowMask,            /* LeaveNotify */
  104.     FocusChangeMask,            /* FocusIn */
  105.     FocusChangeMask,            /* FocusOut */
  106.     KeymapStateMask,            /* KeymapNotify */
  107.     ExposureMask,            /* Expose */
  108.     ExposureMask,            /* GraphicsExpose */
  109.     ExposureMask,            /* NoExpose */
  110.     VisibilityChangeMask,        /* VisibilityNotify */
  111.     SubstructureNotifyMask,        /* CreateNotify */
  112.     StructureNotifyMask,        /* DestroyNotify */
  113.     StructureNotifyMask,        /* UnmapNotify */
  114.     StructureNotifyMask,        /* MapNotify */
  115.     SubstructureRedirectMask,        /* MapRequest */
  116.     StructureNotifyMask,        /* ReparentNotify */
  117.     StructureNotifyMask,        /* ConfigureNotify */
  118.     SubstructureRedirectMask,        /* ConfigureRequest */
  119.     StructureNotifyMask,        /* GravityNotify */
  120.     ResizeRedirectMask,            /* ResizeRequest */
  121.     StructureNotifyMask,        /* CirculateNotify */
  122.     SubstructureRedirectMask,        /* CirculateRequest */
  123.     PropertyChangeMask,            /* PropertyNotify */
  124.     0,                    /* SelectionClear */
  125.     0,                    /* SelectionRequest */
  126.     0,                    /* SelectionNotify */
  127.     ColormapChangeMask,            /* ColormapNotify */
  128.     0,                    /* ClientMessage */
  129.     0,                    /* Mapping Notify */
  130.     VirtualEventMask,            /* VirtualEvents */
  131.     ActivateMask,            /* ActivateNotify */
  132.     ActivateMask            /* DeactivateNotify */
  133. };
  134.  
  135. /*
  136.  * If someone has called Tk_RestrictEvents, the information below
  137.  * keeps track of it.
  138.  */
  139.  
  140. static Tk_RestrictProc *restrictProc;
  141.                 /* Procedure to call.  NULL means no
  142.                  * restrictProc is currently in effect. */
  143. static ClientData restrictArg;    /* Argument to pass to restrictProc. */
  144.  
  145. /*
  146.  * Prototypes for procedures that are only referenced locally within
  147.  * this file.
  148.  */
  149.  
  150. static void        DelayedMotionProc _ANSI_ARGS_((ClientData clientData));
  151. static int        WindowEventProc _ANSI_ARGS_((Tcl_Event *evPtr,
  152.                 int flags));
  153.  
  154. /*
  155.  *--------------------------------------------------------------
  156.  *
  157.  * Tk_CreateEventHandler --
  158.  *
  159.  *    Arrange for a given procedure to be invoked whenever
  160.  *    events from a given class occur in a given window.
  161.  *
  162.  * Results:
  163.  *    None.
  164.  *
  165.  * Side effects:
  166.  *    From now on, whenever an event of the type given by
  167.  *    mask occurs for token and is processed by Tk_HandleEvent,
  168.  *    proc will be called.  See the manual entry for details
  169.  *    of the calling sequence and return value for proc.
  170.  *
  171.  *--------------------------------------------------------------
  172.  */
  173.  
  174. void
  175. Tk_CreateEventHandler(token, mask, proc, clientData)
  176.     Tk_Window token;        /* Token for window in which to
  177.                  * create handler. */
  178.     unsigned long mask;        /* Events for which proc should
  179.                  * be called. */
  180.     Tk_EventProc *proc;        /* Procedure to call for each
  181.                  * selected event */
  182.     ClientData clientData;    /* Arbitrary data to pass to proc. */
  183. {
  184.     register TkEventHandler *handlerPtr;
  185.     register TkWindow *winPtr = (TkWindow *) token;
  186.     int found;
  187.  
  188.     /*
  189.      * Skim through the list of existing handlers to (a) compute the
  190.      * overall event mask for the window (so we can pass this new
  191.      * value to the X system) and (b) see if there's already a handler
  192.      * declared with the same callback and clientData (if so, just
  193.      * change the mask).  If no existing handler matches, then create
  194.      * a new handler.
  195.      */
  196.  
  197.     found = 0;
  198.     if (winPtr->handlerList == NULL) {
  199.     handlerPtr = (TkEventHandler *) ckalloc(
  200.         (unsigned) sizeof(TkEventHandler));
  201.     winPtr->handlerList = handlerPtr;
  202.     goto initHandler;
  203.     } else {
  204.     for (handlerPtr = winPtr->handlerList; ;
  205.         handlerPtr = handlerPtr->nextPtr) {
  206.         if ((handlerPtr->proc == proc)
  207.             && (handlerPtr->clientData == clientData)) {
  208.         handlerPtr->mask = mask;
  209.         found = 1;
  210.         }
  211.         if (handlerPtr->nextPtr == NULL) {
  212.         break;
  213.         }
  214.     }
  215.     }
  216.  
  217.     /*
  218.      * Create a new handler if no matching old handler was found.
  219.      */
  220.  
  221.     if (!found) {
  222.     handlerPtr->nextPtr = (TkEventHandler *)
  223.         ckalloc(sizeof(TkEventHandler));
  224.     handlerPtr = handlerPtr->nextPtr;
  225.     initHandler:
  226.     handlerPtr->mask = mask;
  227.     handlerPtr->proc = proc;
  228.     handlerPtr->clientData = clientData;
  229.     handlerPtr->nextPtr = NULL;
  230.     }
  231.  
  232.     /*
  233.      * No need to call XSelectInput:  Tk always selects on all events
  234.      * for all windows (needed to support bindings on classes and "all").
  235.      */
  236. }
  237.  
  238. /*
  239.  *--------------------------------------------------------------
  240.  *
  241.  * Tk_DeleteEventHandler --
  242.  *
  243.  *    Delete a previously-created handler.
  244.  *
  245.  * Results:
  246.  *    None.
  247.  *
  248.  * Side effects:
  249.  *    If there existed a handler as described by the
  250.  *    parameters, the handler is deleted so that proc
  251.  *    will not be invoked again.
  252.  *
  253.  *--------------------------------------------------------------
  254.  */
  255.  
  256. void
  257. Tk_DeleteEventHandler(token, mask, proc, clientData)
  258.     Tk_Window token;        /* Same as corresponding arguments passed */
  259.     unsigned long mask;        /* previously to Tk_CreateEventHandler. */
  260.     Tk_EventProc *proc;
  261.     ClientData clientData;
  262. {
  263.     register TkEventHandler *handlerPtr;
  264.     register InProgress *ipPtr;
  265.     TkEventHandler *prevPtr;
  266.     register TkWindow *winPtr = (TkWindow *) token;
  267.  
  268.     /*
  269.      * Find the event handler to be deleted, or return
  270.      * immediately if it doesn't exist.
  271.      */
  272.  
  273.     for (handlerPtr = winPtr->handlerList, prevPtr = NULL; ;
  274.         prevPtr = handlerPtr, handlerPtr = handlerPtr->nextPtr) {
  275.     if (handlerPtr == NULL) {
  276.         return;
  277.     }
  278.     if ((handlerPtr->mask == mask) && (handlerPtr->proc == proc)
  279.         && (handlerPtr->clientData == clientData)) {
  280.         break;
  281.     }
  282.     }
  283.  
  284.     /*
  285.      * If Tk_HandleEvent is about to process this handler, tell it to
  286.      * process the next one instead.
  287.      */
  288.  
  289.     for (ipPtr = pendingPtr; ipPtr != NULL; ipPtr = ipPtr->nextPtr) {
  290.     if (ipPtr->nextHandler == handlerPtr) {
  291.         ipPtr->nextHandler = handlerPtr->nextPtr;
  292.     }
  293.     }
  294.  
  295.     /*
  296.      * Free resources associated with the handler.
  297.      */
  298.  
  299.     if (prevPtr == NULL) {
  300.     winPtr->handlerList = handlerPtr->nextPtr;
  301.     } else {
  302.     prevPtr->nextPtr = handlerPtr->nextPtr;
  303.     }
  304.     ckfree((char *) handlerPtr);
  305.  
  306.  
  307.     /*
  308.      * No need to call XSelectInput:  Tk always selects on all events
  309.      * for all windows (needed to support bindings on classes and "all").
  310.      */
  311. }
  312.  
  313. /*--------------------------------------------------------------
  314.  *
  315.  * Tk_CreateGenericHandler --
  316.  *
  317.  *    Register a procedure to be called on each X event, regardless
  318.  *    of display or window.  Generic handlers are useful for capturing
  319.  *    events that aren't associated with windows, or events for windows
  320.  *    not managed by Tk.
  321.  *
  322.  * Results:
  323.  *    None.
  324.  *
  325.  * Side Effects:
  326.  *    From now on, whenever an X event is given to Tk_HandleEvent,
  327.  *    invoke proc, giving it clientData and the event as arguments.
  328.  *
  329.  *--------------------------------------------------------------
  330.  */
  331.  
  332. void
  333. Tk_CreateGenericHandler(proc, clientData)
  334.      Tk_GenericProc *proc;    /* Procedure to call on every event. */
  335.      ClientData clientData;    /* One-word value to pass to proc. */
  336. {
  337.     GenericHandler *handlerPtr;
  338.     
  339.     handlerPtr = (GenericHandler *) ckalloc (sizeof (GenericHandler));
  340.     
  341.     handlerPtr->proc = proc;
  342.     handlerPtr->clientData = clientData;
  343.     handlerPtr->deleteFlag = 0;
  344.     handlerPtr->nextPtr = NULL;
  345.     if (genericList == NULL) {
  346.     genericList = handlerPtr;
  347.     } else {
  348.     lastGenericPtr->nextPtr = handlerPtr;
  349.     }
  350.     lastGenericPtr = handlerPtr;
  351. }
  352.  
  353. /*
  354.  *--------------------------------------------------------------
  355.  *
  356.  * Tk_DeleteGenericHandler --
  357.  *
  358.  *    Delete a previously-created generic handler.
  359.  *
  360.  * Results:
  361.  *    None.
  362.  *
  363.  * Side Effects:
  364.  *    If there existed a handler as described by the parameters,
  365.  *    that handler is logically deleted so that proc will not be
  366.  *    invoked again.  The physical deletion happens in the event
  367.  *    loop in Tk_HandleEvent.
  368.  *
  369.  *--------------------------------------------------------------
  370.  */
  371.  
  372. void
  373. Tk_DeleteGenericHandler(proc, clientData)
  374.      Tk_GenericProc *proc;
  375.      ClientData clientData;
  376. {
  377.     GenericHandler * handler;
  378.     
  379.     for (handler = genericList; handler; handler = handler->nextPtr) {
  380.     if ((handler->proc == proc) && (handler->clientData == clientData)) {
  381.         handler->deleteFlag = 1;
  382.     }
  383.     }
  384. }
  385.  
  386. /*
  387.  *--------------------------------------------------------------
  388.  *
  389.  * Tk_HandleEvent --
  390.  *
  391.  *    Given an event, invoke all the handlers that have
  392.  *    been registered for the event.
  393.  *
  394.  * Results:
  395.  *    None.
  396.  *
  397.  * Side effects:
  398.  *    Depends on the handlers.
  399.  *
  400.  *--------------------------------------------------------------
  401.  */
  402.  
  403. void
  404. Tk_HandleEvent(eventPtr)
  405.     XEvent *eventPtr;        /* Event to dispatch. */
  406. {
  407.     register TkEventHandler *handlerPtr;
  408.     register GenericHandler *genericPtr;
  409.     register GenericHandler *genPrevPtr;
  410.     TkWindow *winPtr;
  411.     unsigned long mask;
  412.     InProgress ip;
  413.     Window handlerWindow;
  414.     TkDisplay *dispPtr;
  415.     Tcl_Interp *interp = (Tcl_Interp *) NULL;
  416.  
  417.     /* 
  418.      * Next, invoke all the generic event handlers (those that are
  419.      * invoked for all events).  If a generic event handler reports that
  420.      * an event is fully processed, go no further.
  421.      */
  422.  
  423.     for (genPrevPtr = NULL, genericPtr = genericList;  genericPtr != NULL; ) {
  424.     if (genericPtr->deleteFlag) {
  425.         if (!genericHandlersActive) {
  426.         GenericHandler *tmpPtr;
  427.  
  428.         /*
  429.          * This handler needs to be deleted and there are no
  430.          * calls pending through the handler, so now is a safe
  431.          * time to delete it.
  432.          */
  433.  
  434.         tmpPtr = genericPtr->nextPtr;
  435.         if (genPrevPtr == NULL) {
  436.             genericList = tmpPtr;
  437.         } else {
  438.             genPrevPtr->nextPtr = tmpPtr;
  439.         }
  440.         if (tmpPtr == NULL) {
  441.             lastGenericPtr = genPrevPtr;
  442.         }
  443.         (void) ckfree((char *) genericPtr);
  444.         genericPtr = tmpPtr;
  445.         continue;
  446.         }
  447.     } else {
  448.         int done;
  449.  
  450.         genericHandlersActive++;
  451.         done = (*genericPtr->proc)(genericPtr->clientData, eventPtr);
  452.         genericHandlersActive--;
  453.         if (done) {
  454.         return;
  455.         }
  456.     }
  457.     genPrevPtr = genericPtr;
  458.     genericPtr = genPrevPtr->nextPtr;
  459.     }
  460.  
  461.     /*
  462.      * If the event is a MappingNotify event, find its display and
  463.      * refresh the keyboard mapping information for the display.
  464.      * After that there's nothing else to do with the event, so just
  465.      * quit.
  466.      */
  467.  
  468.     if (eventPtr->type == MappingNotify) {
  469.     dispPtr = TkGetDisplay(eventPtr->xmapping.display);
  470.     if (dispPtr != NULL) {
  471.         XRefreshKeyboardMapping(&eventPtr->xmapping);
  472.         dispPtr->bindInfoStale = 1;
  473.     }
  474.     return;
  475.     }
  476.  
  477.     /*
  478.      * Events selected by StructureNotify require special handling.
  479.      * They look the same as those selected by SubstructureNotify.
  480.      * The only difference is whether the "event" and "window" fields
  481.      * are the same.  Compare the two fields and convert StructureNotify
  482.      * to SubstructureNotify if necessary.
  483.      */
  484.  
  485.     handlerWindow = eventPtr->xany.window;
  486.     mask = eventMasks[eventPtr->xany.type];
  487.     if (mask == StructureNotifyMask) {
  488.     if (eventPtr->xmap.event != eventPtr->xmap.window) {
  489.         mask = SubstructureNotifyMask;
  490.         handlerWindow = eventPtr->xmap.event;
  491.     }
  492.     }
  493.     winPtr = (TkWindow *) Tk_IdToWindow(eventPtr->xany.display, handlerWindow);
  494.     if (winPtr == NULL) {
  495.  
  496.     /*
  497.      * There isn't a TkWindow structure for this window.
  498.      * However, if the event is a PropertyNotify event then call
  499.      * the selection manager (it deals beneath-the-table with
  500.      * certain properties).
  501.      */
  502.  
  503.     if (eventPtr->type == PropertyNotify) {
  504.         TkSelPropProc(eventPtr);
  505.     }
  506.     return;
  507.     }
  508.  
  509.     /*
  510.      * Once a window has started getting deleted, don't process any more
  511.      * events for it except for the DestroyNotify event.  This check is
  512.      * needed because a DestroyNotify handler could re-invoke the event
  513.      * loop, causing other pending events to be handled for the window
  514.      * (the window doesn't get totally expunged from our tables until
  515.      * after the DestroyNotify event has been completely handled).
  516.      */
  517.  
  518.     if ((winPtr->flags & TK_ALREADY_DEAD)
  519.         && (eventPtr->type != DestroyNotify)) {
  520.     return;
  521.     }
  522.  
  523.     if (winPtr->mainPtr != NULL) {
  524.  
  525.         /*
  526.          * Protect interpreter for this window from possible deletion
  527.          * while we are dealing with the event for this window. Thus,
  528.          * widget writers do not have to worry about protecting the
  529.          * interpreter in their own code.
  530.          */
  531.         
  532.         interp = winPtr->mainPtr->interp;
  533.         Tcl_Preserve((ClientData) interp);
  534.         
  535.     /*
  536.      * Call focus-related code to look at FocusIn, FocusOut, Enter,
  537.      * and Leave events;  depending on its return value, ignore the
  538.      * event.
  539.      */
  540.     
  541.     if ((mask & (FocusChangeMask|EnterWindowMask|LeaveWindowMask))
  542.         && !TkFocusFilterEvent(winPtr, eventPtr)) {
  543.             Tcl_Release((ClientData) interp);
  544.         return;
  545.     }
  546.     
  547.     /*
  548.      * Redirect KeyPress and KeyRelease events to the focus window,
  549.      * or ignore them entirely if there is no focus window.
  550.      */
  551.     
  552.     if (mask & (KeyPressMask|KeyReleaseMask)) {
  553.         winPtr->dispPtr->lastEventTime = eventPtr->xkey.time;
  554.         winPtr = TkFocusKeyEvent(winPtr, eventPtr);
  555.         if (winPtr == NULL) {
  556.                 Tcl_Release((ClientData) interp);
  557.         return;
  558.         }
  559.     }
  560.     
  561.     /*
  562.      * Call a grab-related procedure to do special processing on
  563.      * pointer events.
  564.      */
  565.     
  566.     if (mask & (ButtonPressMask|ButtonReleaseMask|PointerMotionMask
  567.         |EnterWindowMask|LeaveWindowMask)) {
  568.         if (mask & (ButtonPressMask|ButtonReleaseMask)) {
  569.         winPtr->dispPtr->lastEventTime = eventPtr->xbutton.time;
  570.         } else if (mask & PointerMotionMask) {
  571.         winPtr->dispPtr->lastEventTime = eventPtr->xmotion.time;
  572.         } else {
  573.         winPtr->dispPtr->lastEventTime = eventPtr->xcrossing.time;
  574.         }
  575.         if (TkPointerEvent(eventPtr, winPtr) == 0) {
  576.                 goto done;
  577.         }
  578.     }
  579.     }
  580.  
  581. #ifdef TK_USE_INPUT_METHODS
  582.     /*
  583.      * Pass the event to the input method(s), if there are any, and
  584.      * discard the event if the input method(s) insist.  Create the
  585.      * input context for the window if it hasn't already been done
  586.      * (XFilterEvent needs this context).
  587.      */
  588.  
  589.     if (!(winPtr->flags & TK_CHECKED_IC)) {
  590.     if (winPtr->dispPtr->inputMethod != NULL) {
  591.         winPtr->inputContext = XCreateIC(
  592.             winPtr->dispPtr->inputMethod, XNInputStyle,
  593.             XIMPreeditNothing|XIMStatusNothing,
  594.             XNClientWindow, winPtr->window,
  595.             XNFocusWindow, winPtr->window, NULL);
  596.     }
  597.     winPtr->flags |= TK_CHECKED_IC;
  598.     }
  599.     if (XFilterEvent(eventPtr, None)) {
  600.         goto done;
  601.     }
  602. #endif /* TK_USE_INPUT_METHODS */
  603.  
  604.     /*
  605.      * For events where it hasn't already been done, update the current
  606.      * time in the display.
  607.      */
  608.  
  609.     if (eventPtr->type == PropertyNotify) {
  610.     winPtr->dispPtr->lastEventTime = eventPtr->xproperty.time;
  611.     }
  612.  
  613.     /*
  614.      * There's a potential interaction here with Tk_DeleteEventHandler.
  615.      * Read the documentation for pendingPtr.
  616.      */
  617.  
  618.     ip.eventPtr = eventPtr;
  619.     ip.winPtr = winPtr;
  620.     ip.nextHandler = NULL;
  621.     ip.nextPtr = pendingPtr;
  622.     pendingPtr = &ip;
  623.     if (mask == 0) {
  624.     if ((eventPtr->type == SelectionClear)
  625.         || (eventPtr->type == SelectionRequest)
  626.         || (eventPtr->type == SelectionNotify)) {
  627.         TkSelEventProc((Tk_Window) winPtr, eventPtr);
  628.     } else if ((eventPtr->type == ClientMessage)
  629.         && (eventPtr->xclient.message_type ==
  630.             Tk_InternAtom((Tk_Window) winPtr, "WM_PROTOCOLS"))) {
  631.         TkWmProtocolEventProc(winPtr, eventPtr);
  632.     }
  633.     } else {
  634.     for (handlerPtr = winPtr->handlerList; handlerPtr != NULL; ) {
  635.         if ((handlerPtr->mask & mask) != 0) {
  636.         ip.nextHandler = handlerPtr->nextPtr;
  637.         (*(handlerPtr->proc))(handlerPtr->clientData, eventPtr);
  638.         handlerPtr = ip.nextHandler;
  639.         } else {
  640.         handlerPtr = handlerPtr->nextPtr;
  641.         }
  642.     }
  643.  
  644.     /*
  645.      * Pass the event to the "bind" command mechanism.  But, don't
  646.      * do this for SubstructureNotify events.  The "bind" command
  647.      * doesn't support them anyway, and it's easier to filter out
  648.      * these events here than in the lower-level procedures.
  649.      */
  650.  
  651.     if ((ip.winPtr != None) && (mask != SubstructureNotifyMask)) {
  652.         TkBindEventProc(winPtr, eventPtr);
  653.     }
  654.     }
  655.     pendingPtr = ip.nextPtr;
  656. done:
  657.  
  658.     /*
  659.      * Release the interpreter for this window so that it can be potentially
  660.      * deleted if requested.
  661.      */
  662.     
  663.     if (interp != (Tcl_Interp *) NULL) {
  664.         Tcl_Release((ClientData) interp);
  665.     }
  666. }
  667.  
  668. /*
  669.  *--------------------------------------------------------------
  670.  *
  671.  * TkEventDeadWindow --
  672.  *
  673.  *    This procedure is invoked when it is determined that
  674.  *    a window is dead.  It cleans up event-related information
  675.  *    about the window.
  676.  *
  677.  * Results:
  678.  *    None.
  679.  *
  680.  * Side effects:
  681.  *    Various things get cleaned up and recycled.
  682.  *
  683.  *--------------------------------------------------------------
  684.  */
  685.  
  686. void
  687. TkEventDeadWindow(winPtr)
  688.     TkWindow *winPtr;        /* Information about the window
  689.                  * that is being deleted. */
  690. {
  691.     register TkEventHandler *handlerPtr;
  692.     register InProgress *ipPtr;
  693.  
  694.     /*
  695.      * While deleting all the handlers, be careful to check for
  696.      * Tk_HandleEvent being about to process one of the deleted
  697.      * handlers.  If it is, tell it to quit (all of the handlers
  698.      * are being deleted).
  699.      */
  700.  
  701.     while (winPtr->handlerList != NULL) {
  702.     handlerPtr = winPtr->handlerList;
  703.     winPtr->handlerList = handlerPtr->nextPtr;
  704.     for (ipPtr = pendingPtr; ipPtr != NULL; ipPtr = ipPtr->nextPtr) {
  705.         if (ipPtr->nextHandler == handlerPtr) {
  706.         ipPtr->nextHandler = NULL;
  707.         }
  708.         if (ipPtr->winPtr == winPtr) {
  709.         ipPtr->winPtr = None;
  710.         }
  711.     }
  712.     ckfree((char *) handlerPtr);
  713.     }
  714. }
  715.  
  716. /*
  717.  *----------------------------------------------------------------------
  718.  *
  719.  * TkCurrentTime --
  720.  *
  721.  *    Try to deduce the current time.  "Current time" means the time
  722.  *    of the event that led to the current code being executed, which
  723.  *    means the time in the most recently-nested invocation of
  724.  *    Tk_HandleEvent.
  725.  *
  726.  * Results:
  727.  *    The return value is the time from the current event, or
  728.  *    CurrentTime if there is no current event or if the current
  729.  *    event contains no time.
  730.  *
  731.  * Side effects:
  732.  *    None.
  733.  *
  734.  *----------------------------------------------------------------------
  735.  */
  736.  
  737. Time
  738. TkCurrentTime(dispPtr)
  739.     TkDisplay *dispPtr;        /* Display for which the time is desired. */
  740. {
  741.     register XEvent *eventPtr;
  742.  
  743.     if (pendingPtr == NULL) {
  744.     return dispPtr->lastEventTime;
  745.     }
  746.     eventPtr = pendingPtr->eventPtr;
  747.     switch (eventPtr->type) {
  748.     case ButtonPress:
  749.     case ButtonRelease:
  750.         return eventPtr->xbutton.time;
  751.     case KeyPress:
  752.     case KeyRelease:
  753.         return eventPtr->xkey.time;
  754.     case MotionNotify:
  755.         return eventPtr->xmotion.time;
  756.     case EnterNotify:
  757.     case LeaveNotify:
  758.         return eventPtr->xcrossing.time;
  759.     case PropertyNotify:
  760.         return eventPtr->xproperty.time;
  761.     }
  762.     return dispPtr->lastEventTime;
  763. }
  764.  
  765. /*
  766.  *----------------------------------------------------------------------
  767.  *
  768.  * Tk_RestrictEvents --
  769.  *
  770.  *    This procedure is used to globally restrict the set of events
  771.  *    that will be dispatched.  The restriction is done by filtering
  772.  *    all incoming X events through a procedure that determines
  773.  *    whether they are to be processed immediately, deferred, or
  774.  *    discarded.
  775.  *
  776.  * Results:
  777.  *    The return value is the previous restriction procedure in effect,
  778.  *    if there was one, or NULL if there wasn't.
  779.  *
  780.  * Side effects:
  781.  *    From now on, proc will be called to determine whether to process,
  782.  *    defer or discard each incoming X event.
  783.  *
  784.  *----------------------------------------------------------------------
  785.  */
  786.  
  787. Tk_RestrictProc *
  788. Tk_RestrictEvents(proc, arg, prevArgPtr)
  789.     Tk_RestrictProc *proc;    /* Procedure to call for each incoming
  790.                  * event. */
  791.     ClientData arg;        /* Arbitrary argument to pass to proc. */
  792.     ClientData *prevArgPtr;    /* Place to store information about previous
  793.                  * argument. */
  794. {
  795.     Tk_RestrictProc *prev;
  796.  
  797.     prev = restrictProc;
  798.     *prevArgPtr = restrictArg;
  799.     restrictProc = proc;
  800.     restrictArg = arg;
  801.     return prev;
  802. }
  803.  
  804. /*
  805.  *----------------------------------------------------------------------
  806.  *
  807.  * Tk_QueueWindowEvent --
  808.  *
  809.  *    Given an X-style window event, this procedure adds it to the
  810.  *    Tcl event queue at the given position.  This procedure also
  811.  *    performs mouse motion event collapsing if possible.
  812.  *
  813.  * Results:
  814.  *    None.
  815.  *
  816.  * Side effects:
  817.  *    Adds stuff to the event queue, which will eventually be
  818.  *    processed.
  819.  *
  820.  *----------------------------------------------------------------------
  821.  */
  822.  
  823. void
  824. Tk_QueueWindowEvent(eventPtr, position)
  825.     XEvent *eventPtr;            /* Event to add to queue.  This
  826.                      * procedures copies it before adding
  827.                      * it to the queue. */
  828.     Tcl_QueuePosition position;        /* Where to put it on the queue:
  829.                      * TCL_QUEUE_TAIL, TCL_QUEUE_HEAD,
  830.                      * or TCL_QUEUE_MARK. */
  831. {
  832.     TkWindowEvent *wevPtr;
  833.     TkDisplay *dispPtr;
  834.  
  835.     /*
  836.      * Find our display structure for the event's display.
  837.      */
  838.  
  839.     for (dispPtr = tkDisplayList; ; dispPtr = dispPtr->nextPtr) {
  840.     if (dispPtr == NULL) {
  841.         return;
  842.     }
  843.     if (dispPtr->display == eventPtr->xany.display) {
  844.         break;
  845.     }
  846.     }
  847.  
  848.     if ((dispPtr->delayedMotionPtr != NULL) && (position == TCL_QUEUE_TAIL)) {
  849.     if ((eventPtr->type == MotionNotify) && (eventPtr->xmotion.window
  850.         == dispPtr->delayedMotionPtr->event.xmotion.window)) {
  851.         /*
  852.          * The new event is a motion event in the same window as the
  853.          * saved motion event.  Just replace the saved event with the
  854.          * new one.
  855.          */
  856.  
  857.         dispPtr->delayedMotionPtr->event = *eventPtr;
  858.         return;
  859.     } else if ((eventPtr->type != GraphicsExpose)
  860.         && (eventPtr->type != NoExpose)
  861.         && (eventPtr->type != Expose)) {
  862.         /*
  863.          * The new event may conflict with the saved motion event.  Queue
  864.          * the saved motion event now so that it will be processed before
  865.          * the new event.
  866.          */
  867.  
  868.         Tcl_QueueEvent(&dispPtr->delayedMotionPtr->header, position);
  869.         dispPtr->delayedMotionPtr = NULL;
  870.         Tcl_CancelIdleCall(DelayedMotionProc, (ClientData) dispPtr);
  871.     }
  872.     }
  873.  
  874.     wevPtr = (TkWindowEvent *) ckalloc(sizeof(TkWindowEvent));
  875.     wevPtr->header.proc = WindowEventProc;
  876.     wevPtr->event = *eventPtr;
  877.     if ((eventPtr->type == MotionNotify) && (position == TCL_QUEUE_TAIL)) {
  878.     /*
  879.      * The new event is a motion event so don't queue it immediately;
  880.      * save it around in case another motion event arrives that it can
  881.      * be collapsed with.
  882.      */
  883.  
  884.     if (dispPtr->delayedMotionPtr != NULL) {
  885.         panic("Tk_QueueWindowEvent found unexpected delayed motion event");
  886.     }
  887.     dispPtr->delayedMotionPtr = wevPtr;
  888.     Tcl_DoWhenIdle(DelayedMotionProc, (ClientData) dispPtr);
  889.     } else {
  890.     Tcl_QueueEvent(&wevPtr->header, position);
  891.     }
  892. }
  893.  
  894. /*
  895.  *---------------------------------------------------------------------------
  896.  *
  897.  * TkQueueEventForAllChildren --
  898.  *
  899.  *    Given an XEvent, recursively queue the event for this window and
  900.  *    all non-toplevel children of the given window.  
  901.  *
  902.  * Results:
  903.  *    None.
  904.  *
  905.  * Side effects:
  906.  *    Events queued.
  907.  *
  908.  *---------------------------------------------------------------------------
  909.  */
  910.  
  911. void
  912. TkQueueEventForAllChildren(winPtr, eventPtr)
  913.     TkWindow *winPtr;        /* Window to which event is sent. */
  914.     XEvent *eventPtr;        /* The event to be sent. */
  915. {
  916.     TkWindow *childPtr;
  917.  
  918.     eventPtr->xany.window = winPtr->window;
  919.     Tk_QueueWindowEvent(eventPtr, TCL_QUEUE_TAIL);
  920.     
  921.     childPtr = winPtr->childList;
  922.     while (childPtr != NULL) {
  923.     if (!Tk_IsTopLevel(childPtr)) {
  924.         TkQueueEventForAllChildren(childPtr, eventPtr);
  925.     }
  926.     childPtr = childPtr->nextPtr;
  927.     }
  928. }
  929.  
  930. /*
  931.  *----------------------------------------------------------------------
  932.  *
  933.  * WindowEventProc --
  934.  *
  935.  *    This procedure is called by Tcl_DoOneEvent when a window event
  936.  *    reaches the front of the event queue.  This procedure is responsible
  937.  *    for actually handling the event.
  938.  *
  939.  * Results:
  940.  *    Returns 1 if the event was handled, meaning it should be removed
  941.  *    from the queue.  Returns 0 if the event was not handled, meaning
  942.  *    it should stay on the queue.  The event isn't handled if the
  943.  *    TCL_WINDOW_EVENTS bit isn't set in flags, if a restrict proc
  944.  *    prevents the event from being handled.
  945.  *
  946.  * Side effects:
  947.  *    Whatever the event handlers for the event do.
  948.  *
  949.  *----------------------------------------------------------------------
  950.  */
  951.  
  952. static int
  953. WindowEventProc(evPtr, flags)
  954.     Tcl_Event *evPtr;        /* Event to service. */
  955.     int flags;            /* Flags that indicate what events to
  956.                  * handle, such as TCL_WINDOW_EVENTS. */
  957. {
  958.     TkWindowEvent *wevPtr = (TkWindowEvent *) evPtr;
  959.     Tk_RestrictAction result;
  960.  
  961.     if (!(flags & TCL_WINDOW_EVENTS)) {
  962.     return 0;
  963.     }
  964.     if (restrictProc != NULL) {
  965.     result = (*restrictProc)(restrictArg, &wevPtr->event);
  966.     if (result != TK_PROCESS_EVENT) {
  967.         if (result == TK_DEFER_EVENT) {
  968.         return 0;
  969.         } else {
  970.         /*
  971.          * TK_DELETE_EVENT: return and say we processed the event,
  972.          * even though we didn't do anything at all.
  973.          */
  974.         return 1;
  975.         }
  976.     }
  977.     }
  978.     Tk_HandleEvent(&wevPtr->event);
  979.     return 1;
  980. }
  981.  
  982. /*
  983.  *----------------------------------------------------------------------
  984.  *
  985.  * DelayedMotionProc --
  986.  *
  987.  *    This procedure is invoked as an idle handler when a mouse motion
  988.  *    event has been delayed.  It queues the delayed event so that it
  989.  *    will finally be serviced.
  990.  *
  991.  * Results:
  992.  *    None.
  993.  *
  994.  * Side effects:
  995.  *    The delayed mouse motion event gets added to the Tcl event
  996.  *    queue for servicing.
  997.  *
  998.  *----------------------------------------------------------------------
  999.  */
  1000.  
  1001. static void
  1002. DelayedMotionProc(clientData)
  1003.     ClientData clientData;    /* Pointer to display containing a delayed
  1004.                  * motion event to be serviced. */
  1005. {
  1006.     TkDisplay *dispPtr = (TkDisplay *) clientData;
  1007.  
  1008.     if (dispPtr->delayedMotionPtr == NULL) {
  1009.     panic("DelayedMotionProc found no delayed mouse motion event");
  1010.     }
  1011.     Tcl_QueueEvent(&dispPtr->delayedMotionPtr->header, TCL_QUEUE_TAIL);
  1012.     dispPtr->delayedMotionPtr = NULL;
  1013. }
  1014.  
  1015. /*
  1016.  *--------------------------------------------------------------
  1017.  *
  1018.  * Tk_MainLoop --
  1019.  *
  1020.  *    Call Tcl_DoOneEvent over and over again in an infinite
  1021.  *    loop as long as there exist any main windows.
  1022.  *
  1023.  * Results:
  1024.  *    None.
  1025.  *
  1026.  * Side effects:
  1027.  *    Arbitrary;  depends on handlers for events.
  1028.  *
  1029.  *--------------------------------------------------------------
  1030.  */
  1031.  
  1032. void
  1033. Tk_MainLoop()
  1034. {
  1035.     while (Tk_GetNumMainWindows() > 0) {
  1036.     Tcl_DoOneEvent(0);
  1037.     }
  1038. }
  1039.